package gamePackage;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;


public class View extends JFrame{

	private JPanel titleScreen, tileScreen;
	private ArrayList<JLabel> titleScreenMenu, tiles;
	private JLabel gameTitle, newGame, loadGame, options;
	
	final static int HIGHLIGHED_BORDER_SIZE = 2;
	
	private int currentlySelected;
	
	public void getTitleScreen(){
		
		titleScreen = new JPanel(null);
		
		titleScreenMenu = new ArrayList<JLabel>();
		titleScreenMenu.add(getNewGame());
		titleScreenMenu.add(getLoadGame());
		titleScreenMenu.add(getOptions());
		
		for(int count = 0; count < titleScreenMenu.size(); count++){
			titleScreenMenu.get(count).addMouseListener(new TitleScreenListener());
		}
		

		titleScreen.add(getGameTitle());
		titleScreen.add(newGame);
		titleScreen.add(loadGame);
		titleScreen.add(options);
		
		currentlySelected = 0;
		setMenuBorders(titleScreenMenu);
		
		this.addKeyListener(new TitleScreenListener());
		
		return;
	}
	
	public void getTileScreen(){
		
		tileScreen = new JPanel(null);
		tiles = new ArrayList<JLabel>();
		JLabel tempTile;
		
		for(int count = 0; count < Controller.NUM_TILES*Controller.NUM_TILES; count++){
			tempTile = this.getTileLabel(count%Controller.NUM_TILES, count/Controller.NUM_TILES);
			tempTile.addMouseListener(new TileListener());
			
			tiles.add(tempTile);
			tileScreen.add(tempTile);
		}
		
	}
	
	public JLabel getTileLabel(int x, int y){
		
		JLabel tempTile = new JLabel();
		tempTile.setBounds(new Rectangle(x*Controller.TILE_SIZE, y*Controller.TILE_SIZE, Controller.TILE_SIZE, Controller.TILE_SIZE));
		tempTile.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		tempTile.setBackground(Color.GREEN);
		tempTile.setOpaque(true);
		return tempTile;
		
	}
	
	private JLabel getGameTitle(){
		
		gameTitle = new JLabel("Fibonacci--v0.2");
		gameTitle.setBounds(new Rectangle(100, 50, 100, 25));
		gameTitle.setHorizontalAlignment(SwingConstants.CENTER);
		return gameTitle;
	}
	
	private JLabel getNewGame(){

		newGame = new JLabel("New Game");
		newGame.setBounds(new Rectangle(100, 100, 100, 25));
		newGame.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		newGame.setHorizontalTextPosition(JLabel.LEADING);
		newGame.setHorizontalAlignment(SwingConstants.CENTER);
		return newGame;
	}

	private JLabel getLoadGame(){

		loadGame = new JLabel("Load Game");
		loadGame.setBounds(new Rectangle(100, 150, 100, 25));
		loadGame.setHorizontalAlignment(SwingConstants.CENTER);
		return loadGame;
	}
	
	private JLabel getOptions(){

		options = new JLabel("Options");
		options.setBounds(new Rectangle(100, 200, 100, 25));
		options.setHorizontalAlignment(SwingConstants.CENTER);
		return options;
	}
	
	public void setToTitleScreen(){
		
		if(titleScreen == null){
			this.getTitleScreen();
		}
		this.setContentPane(titleScreen);
		this.setVisible(true);
		this.currentlySelected = 0;
	}

	public void setToTileScreen(){
		
		if(tileScreen == null){
			this.getTileScreen();
		}
		this.setContentPane(tileScreen);
		this.setVisible(true);
		this.currentlySelected = 0;
		
	}
	
	private void setMenuBorders(ArrayList<JLabel> items){
		
		for(int count = 0; count < items.size(); count++){
			if(count == currentlySelected){
				if(items.get(count).getBorder() instanceof LineBorder){
					items.get(count).setBounds(
							new Rectangle(items.get(count).getX()-View.HIGHLIGHED_BORDER_SIZE,
									items.get(count).getY()-View.HIGHLIGHED_BORDER_SIZE,
									items.get(count).getWidth()+View.HIGHLIGHED_BORDER_SIZE,
									items.get(count).getHeight()+View.HIGHLIGHED_BORDER_SIZE));
				items.get(count).setBorder(
						BorderFactory.createCompoundBorder(
								BorderFactory.createLineBorder(Color.YELLOW, View.HIGHLIGHED_BORDER_SIZE), 
								BorderFactory.createLineBorder(Color.BLACK)));
				}
			}
			else{
				if(items.get(count).getBorder() instanceof CompoundBorder){
					items.get(count).setBounds(
							new Rectangle(items.get(count).getX()+View.HIGHLIGHED_BORDER_SIZE,
									items.get(count).getY()+View.HIGHLIGHED_BORDER_SIZE,
									items.get(count).getWidth()-View.HIGHLIGHED_BORDER_SIZE,
									items.get(count).getHeight()-View.HIGHLIGHED_BORDER_SIZE));
				}
				items.get(count).setBorder(BorderFactory.createLineBorder(Color.BLACK));
			}
		}
		return;
	}
	
	private void setEnterTileBorders(JLabel item){
		
		item.setBorder(BorderFactory.createLineBorder(Color.BLUE, HIGHLIGHED_BORDER_SIZE));
	}

	private void setExitTileBorders(JLabel item){

		item.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}
	
	private void setTileColor(JLabel item){

		tiles.get(currentlySelected).setBackground(Color.GREEN);
		
		item.setBackground(Color.CYAN);
		item.setOpaque(true);
		
	}
	
	class TitleScreenListener implements KeyListener, MouseListener{

		@Override
		public void keyPressed(KeyEvent e) {

			if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN){
				if(e.getKeyCode() == KeyEvent.VK_UP){
					if(currentlySelected == 0){
						currentlySelected = titleScreenMenu.size()-1;
					}
					else{
						currentlySelected--;
					}
				}
				else if(e.getKeyCode() == KeyEvent.VK_DOWN){
					if(currentlySelected == titleScreenMenu.size()-1){
						currentlySelected = 0;
					}
					else{
						currentlySelected++;
					}
				}
				setMenuBorders(titleScreenMenu);
			}
			else if(e.getKeyCode() == KeyEvent.VK_ENTER){
				mouseClicked(new MouseEvent(titleScreenMenu.get(currentlySelected), 1, 0, 0, 0, 0, 0, false));
			}
			return;
		}

		@Override
		public void keyReleased(KeyEvent e) {
			return;
		}

		@Override
		public void keyTyped(KeyEvent e) {
			return;
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			
			JLabel tempLabel = (JLabel)e.getComponent();
			if(tempLabel.equals(newGame)){
				Controller.setCurrentState(Controller.NEW_GAME_STATE);
				System.out.println("New Game. Current state is now " + Controller.currentState);
			}
			else if(tempLabel.equals(loadGame)){
				Controller.setCurrentState(Controller.LOAD_GAME_STATE);
				System.out.println("Load Game. Current state is now " + Controller.currentState);
			}
			else if(tempLabel.equals(options)){
				Controller.setCurrentState(Controller.OPTIONS_STATE);
				System.out.println("Options. Current state is now " + Controller.currentState);
			}
			return;
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			
			JLabel tempItem = (JLabel)e.getComponent();
			if(tempItem.equals(newGame)){
				currentlySelected = 0;
			}
			else if(tempItem.equals(loadGame)){
				currentlySelected = 1;
			}
			else if(tempItem.equals(options)){
				currentlySelected = 2;
			}
			setMenuBorders(titleScreenMenu);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			return;
		}

		@Override
		public void mousePressed(MouseEvent e) {
			return;
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			return;
		}
		
	}
	
	class TileListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
			
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			
			setEnterTileBorders((JLabel)e.getComponent());
		}

		@Override
		public void mouseExited(MouseEvent e) {
			
			setExitTileBorders((JLabel)e.getComponent());
		}

		@Override
		public void mousePressed(MouseEvent e) {
			
			JLabel tempTile = (JLabel)e.getComponent();
			setTileColor(tempTile);
			currentlySelected = tempTile.getX()/Controller.TILE_SIZE + (tempTile.getY()/Controller.TILE_SIZE)*Controller.NUM_TILES;
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}
		
	}
}
